<?php

/**
 * This is the model class for table "{{gallery}}".
 *
 * The followings are the available columns in table '{{gallery}}':
 * @property integer $id
 * @property string $name
 * @property string $anons
 * @property string $alias
 * @property integer $category_id
 * @property string $metatitle
 * @property string $metakey
 * @property string $metadesc
 * @property string $crdate
 * @property string $file
 * @property integer $url_id
 */
class Gallery extends BaseModel
{
	public $route = 'gallery/view';
	public $get_param = 'category;item';
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Gallery the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{gallery}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, alias, category_id', 'required'),
			array('category_id, url_id', 'numerical', 'integerOnly'=>true),
			array('name, metatitle, metakey, metadesc, file', 'length', 'max'=>1000),
			array('alias', 'length', 'max'=>255),
			array('anons, crdate', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, anons, alias, category_id, metatitle, metakey, metadesc, crdate, file, url_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'url' => array(self::BELONGS_TO, 'Urls', 'url_id'),
			'category' => array(self::BELONGS_TO, 'CategoryContent', 'category_id'),
			'lns' => array(self::HAS_MANY, 'GalleryLn', 'content_id'),
			'ln' => array(self::HAS_ONE, 'GalleryLn', 'content_id',
				'condition'=>'ln.ln=:ln',
				'params'=>array(':ln'=>Yii::app()->language),
			),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' 			=>	Yii::t('main', 'ID'),
			'name' 			=>	Yii::t('main', 'Название'),
			'anons' 		=>	Yii::t('main', 'Анонс'),
			'alias' 		=>	Yii::t('main', 'Синоним'),
			'category_id' 	=>	Yii::t('main', 'Категория'),
			'metatitle' 	=>	Yii::t('main', 'Meta Title'),
			'metakey' 		=>	Yii::t('main', 'Meta Key'),
			'metadesc' 		=>	Yii::t('main', 'Meta Desc'),
			'file' 			=>	Yii::t('main', 'Файл'),
			'crdate' 		=>	Yii::t('main', 'Дата создания'),
			'url_id'		=>	Yii::t('main', 'Ссылка'),
		);
	}

	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord){
				$this->crdate=date('Y-m-d H:i:s');
			}
			return true;
		}
		else
			return false;
	}

	protected function beforeDelete()
	{
		if(parent::beforeDelete())
		{
			Urls::model()->deleteAll(
				'id=:url_id',
				array(':url_id'=>$this->url_id)
			);
			return true;
		}
		else
			return false;
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('anons',$this->anons,true);
		$criteria->compare('alias',$this->alias,true);
		$criteria->compare('category_id',$this->category_id);
		$criteria->compare('metatitle',$this->metatitle,true);
		$criteria->compare('metakey',$this->metakey,true);
		$criteria->compare('metadesc',$this->metadesc,true);
		$criteria->compare('crdate',$this->crdate,true);
		$criteria->compare('file',$this->file,true);
		$criteria->compare('url_id',$this->url_id);

		return new CActiveDataProvider($this, array(
			'pagination'=>array(
					'pageSize'=> Yii::app()->user->getState('pageSize', Yii::app()->params['defaultPageSize']),
			),
			'criteria'=>$criteria,
		));
	}
}